package com.woniu.adapter;

import com.woniu.outlet.mysql.BuyRecordDao;
import com.woniu.outlet.mysql.po.BuyRecordMySQL;
import com.woniu.service.bean.AddBuyRecord;
import com.woniu.service.bean.UpdateBuyRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BuyRecordAdapter {

    @Autowired
    private BuyRecordDao buyRecordDao;

    /**
     * 添加买入记录到数据库的方法
     * @param addOrderSucceed
     */
    public int addOrderSucceed(AddBuyRecord addOrderSucceed) {
        BuyRecordMySQL buyRecordMySQL = new BuyRecordMySQL();
        BeanUtils.copyProperties(addOrderSucceed, buyRecordMySQL);
        buyRecordDao.insertSelective(buyRecordMySQL);
        return buyRecordMySQL.getId();
    }

    /**
     * 向数据表添加手续费
     * @param updateBuyRecord
     */
    public void updateBuyRecord(UpdateBuyRecord updateBuyRecord){
        BuyRecordMySQL buyRecordMySQL = new BuyRecordMySQL();
        BeanUtils.copyProperties(updateBuyRecord,buyRecordMySQL);
        buyRecordDao.updateByPrimaryKeySelective(buyRecordMySQL);
    }

    /**
     * 根据id查询
     */
    public AddBuyRecord getById(Integer id){
        BuyRecordMySQL buyRecordMySQL = buyRecordDao.selectByPrimaryKey(id);
        AddBuyRecord addBuyRecord = new AddBuyRecord();
        BeanUtils.copyProperties(buyRecordMySQL,addBuyRecord);
        return addBuyRecord;
    }

    /**
     * 根据id删除记录
     */
    public void deleteById(Integer id){
        buyRecordDao.deleteByPrimaryKey(id);
    }
}
